package testLayer;

import static org.junit.Assert.*;

import modelLayer.Class;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import dbLayer.DBClass;
import dbLayer.DBTeacher;

public class ClassDatabaseTest {
	
	private DBClass dbClass;
	private boolean retrieveAssociation = true;
	
	@Before
	public void setUp() throws Exception {
		
		this.dbClass = new DBClass();
		this.insert();
		
	}
	
	@After
	public void tearDown() throws Exception {
		
		this.remove();
		this.dbClass = null;
		
	}
	
	public void insert() throws Exception {
		
		this.dbClass.insert("men0813", "1", "2013", new DBTeacher().findById(1));
		
	}
	
	@Test
	public void find() throws Exception {
		
		if (retrieveAssociation) {
			assertTrue(this.dbClass.find("men0813", retrieveAssociation).getClassTeacher() != null);
		}
		else {
			assertNull(this.dbClass.find("men0813", retrieveAssociation));
		}
		
	}
	
	@Test
	public void findById() throws Exception {
		
		if (retrieveAssociation) {
			assertTrue(this.dbClass.findById(this.dbClass.find("men0813", false).getId(),
									  		 retrieveAssociation).getClassTeacher() != null);
		}
		else {
			assertTrue(this.dbClass.findById(this.dbClass.find("men0813", false).getId(),
											 retrieveAssociation) != null);
		}
		
	}
	
	@Test
	public void getAll() throws Exception {
		
		boolean test = true;
		if (retrieveAssociation) {		
			for (Class c : this.dbClass.getAll(retrieveAssociation)) {
				if (c.getClassTeacher() == null) {
					test = false;
				}
			}
		}
		else {
			if (this.dbClass.getAll(retrieveAssociation).isEmpty() &&
				!(this.dbClass.getAll(retrieveAssociation).size() >= 1)) {
				test = true;
			}
		}
		
		assertTrue(test);
		
	}
	
	@Test
	public void update() throws Exception {
		
		assertTrue(this.dbClass.update(this.dbClass.find("men0813", true)) > 0);
		
	}
	
	public void remove() throws Exception {
		
		this.dbClass.remove(this.dbClass.find("men0813", false).getId());
		
	} 

}
